package com.cumt.threeway.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cumt.threeway.R;
import com.cumt.threeway.constant.ThreeWayConstant;
import com.cumt.threeway.table.CommentEntity;
import com.cumt.threeway.util.ThreeWayUtil;
import com.cumt.threeway.view.widget.CircleImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 胡晶晶 on 2016/5/22.
 */
public class NewCommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<CommentEntity> mList;
    private Context mContext;

    public NewCommentAdapter(List<CommentEntity> list, Context context) {
        mContext = context;
        mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = LayoutInflater.from(mContext).inflate(R.layout.comment_ite, parent, false);
        return new ViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        CommentEntity activities = mList.get(position);
        if(!TextUtils.isEmpty(activities.getUserName())) {
            viewHolder.mTvName.setText(activities.getUserName());
        } else {
            viewHolder.mTvName.setText(ThreeWayUtil.getInstance().getUserName(mContext));
        }
        viewHolder.mTvDate.setText(activities.getTime());
        viewHolder.mTvContent.setText(activities.getCommentContent());
        if(!TextUtils.isEmpty(activities.getUserIcon())) {
            Picasso.with(mContext).load(activities.getUserIcon()).into(viewHolder.mIvIcon);
        } else {
            String path = ThreeWayConstant.PATH + ThreeWayUtil.getInstance().getLoginAccount(mContext) + "head.jpg";
            Bitmap bitmap = ThreeWayUtil.getInstance().createChangedImage(path);
            if(bitmap != null) {
                viewHolder.mIvIcon.setImageBitmap(bitmap);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView mTvName;
        private TextView mTvDate;
        private TextView mTvContent;
        private CircleImageView mIvIcon;

        public ViewHolder(View itemView) {
            super(itemView);
            mTvName = (TextView) itemView.findViewById(R.id.tv_pinglun_username);
            mTvDate = (TextView) itemView.findViewById(R.id.tv_pinglun_shijian);
            mTvContent = (TextView) itemView.findViewById(R.id.tv_pinglun_content);
            mIvIcon = (CircleImageView) itemView.findViewById(R.id.iv_image);
        }
    }
}
